package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.BusinessCategoryDao;
import com.qc.entity.BusinessCategoryEntity;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("businessCategoryDao")
public class BusinessCategoryDaoImpl implements BusinessCategoryDao{

	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int getListBusinessCategoryCount(String name) {
		
		String sql = "select count(*) from t_business_category where 1 = 1 and if_del = '0' and level = '1' ";
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<BusinessCategoryEntity> getAllBusinessCategory(String name, int pageNo, int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
        String sql = "select * from t_business_category where 1 = 1 and if_del = '0' and level = '1' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessCategoryEntity.class));
	}

	@Override
	public int saveBusinessCategory(BusinessCategoryEntity businessCategory) {
		
		String sql = "insert into t_business_category(name,parent,level,imgUrl) values(?,?,?,?)";
		
        return jdbcTemplate.update(sql,new Object[]{businessCategory.getName(),businessCategory.getParent(),businessCategory.getLevel(),businessCategory.getImgUrl()});
	}

	@Override
	public int deleteBusinessCategoryId(long id) {
		
		String sql = "update t_business_category set if_del = '1' where b_id = " +id;
        return jdbcTemplate.update(sql);
	}

	@Override
	public int updateBusinessCategoryId(BusinessCategoryEntity businessCategory) {
		
		String sql = "update t_business_category set name = ?,imgUrl = ? where b_id = ? ";
		
        return jdbcTemplate.update(sql,new Object[]{businessCategory.getName(),businessCategory.getImgUrl(),businessCategory.getbId()});
	}

	@Override
	public List<BusinessCategoryEntity> getAllLevleOne() {

		String sql = "select * from t_business_category where 1 = 1 and if_del = '0' and level = '1' ";
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessCategoryEntity.class));
	}
	
	public List<BusinessCategoryEntity> getAllLevleTwo() {

		String sql = "select * from t_business_category where 1 = 1 and if_del = '0' and level = '2' ";
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessCategoryEntity.class));
	}

	@Override
	public int getListBusinessCategoryCountTwo(String name, String parent) {
		
		String sql = "select count(*) from t_business_category where 1 = 1 and if_del = '0' and level = '2' ";
		
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        if (StringUtil.isNotEmpty(parent) && !"undefined".equals(parent) ) {
            sql += " and parent ='"+parent+"' ";
        }
        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<BusinessCategoryEntity> getAllBusinessCategoryTwo(String name, String parent, int pageNo,int pageSize) {
		
		int beginRow = (pageNo - 1)* pageSize;
        String sql = "select * from t_business_category where 1 = 1 and if_del = '0' and level = '2' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        if (StringUtil.isNotEmpty(parent) && !"undefined".equals(parent) ) {
            sql += " and parent ='"+parent+"' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessCategoryEntity.class));
	}

	@Override
	public List<BusinessCategoryEntity> getAllLevleTwo(String parent) {
		
		String sql = "select b_id,name,imgUrl from t_business_category where 1 = 1 and if_del = '0' and level = '2' ";
        
        if (StringUtil.isNotEmpty(parent) && !"undefined".equals(parent) ) {
            sql += " and parent ='"+parent+"' ";
        }
        
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BusinessCategoryEntity.class));
	}

	@Override
	public BusinessCategoryEntity getBusinessCategoryEntityId(long id) {
		
		String sql = "select * from t_business_category where b_id = " + id;
		
		return (BusinessCategoryEntity) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(BusinessCategoryEntity.class));
	}

}
